package cn.zbmao.web.controller.user;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.util.RegexUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import cn.zbmao.web.util.mail.SignupMail;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.Restful;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Before(Restful.class)
public class SignupController extends Controller {
    public void index() {
        setAttr("title","注册");
        setAttr("param","register");
        render("/views/account/register.html");
    }

    public void save() {
        Map<String,String> rsm = null;
        int errno;
        String err;

        String commit = getPara("agreement_chk");

        String name = getPara("user_name");


        String password = getPara("password");
        String pwd=getPara("pwd");
        String email = getPara("email");

        String phone = getPara("phone");
        String website = getPara("website");
        boolean isOK = true;
        String captcha = getPara("seccode_verify",null);

//注册条件判断
        String sessionCaptcha = getSessionAttr("sessionCaptcha");
        if (StringUtils.isEmpty(captcha)||!captcha.equals(sessionCaptcha)){
            setAttr("err","验证码不正确");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }

        if (name != null ) {

            boolean isUser = DaoFactory.getUserDao().isUserSignup(name);
            if (isUser) {

                setAttr("err","用户名已存在!");
                setAttr("errno",-1);
                setAttr("rsm",rsm);
                renderJson();
                return;
            } else {
                int namelength = name.length();
                if ( namelength > 17||namelength<2) {

                    setAttr("err","用户名长度错误!");
                    setAttr("errno",-1);
                    setAttr("rsm",rsm);
                    renderJson();
                    return;
                }
            }
        }

        if (password.length() > 16 || password.length() < 6) {
            setAttr("err", "密码长度错误！");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }
        if(!password.equals(pwd)){
            setAttr("err", "两次密码不相同！");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }
        if (phone==null ||!RegexUtil.isPhone(phone)) {
            setAttr("err", "手机号码错误");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }
        if (website.length()>100) {
            setAttr("err", "网站地址太长！");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }

        if (!RegexUtil.isEmail(email)||email.length()>50) {
            setAttr("err", "邮件格式错误！");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
            return;
        }else
        {
            boolean bl = DaoFactory.getUserDao().isUserEmail(email);
            if (bl) {
                setAttr("err", "邮件已经注册!");
                setAttr("errno",-1);
                setAttr("rsm",rsm);
                renderJson();
                return;
            }
        }
//注册数据存入数据库
        if (commit.equals("agree") && isOK) {
            User user = new User().set("user_name", name).set("user_pwd", password)
                    .set("user_email", email).set(
                            "user_lastlogin_time", new Date()).set(
                            "user_signup_time", new Date())
                    .set("user_signup_mail", 0)
                    .set("account_matured_time",new Date(new Date().getTime()+3600000*24*7))
                    .set("phone", phone)
                    .set("website", website);
            DaoFactory.getUserDao().saveUser(
                    user);
//            注册后发送邮件
            new SignupMail().sendSignupMail(user);
            rsm = new HashMap<String, String>();
            rsm.put("url","/signup/valid_email");
//            rsm.put("email",email);
            getSession().setAttribute("valid_email",email);
            setAttr("err", "注册成功，已经向您的邮箱发送邮件认证。");
            setAttr("errno",1);
            setAttr("rsm",rsm);
            renderJson();

        } else {
            setAttr("err", "未知错误");
            setAttr("errno",-1);
            setAttr("rsm",rsm);
            renderJson();
        }
    }
//    邮件认证后直接进入news页面
    public void auth(){
        int user_id = getParaToInt("keyid");
        String md5 = getPara("key");
        if(DaoFactory.getUserDao().autoUser(user_id, md5)){
            DaoFactory.getUserDao().autoEmail(user_id);
            setAttr("error", "邮箱认证成功，请登录！");
            User user = DaoFactory.getUserDao().getUserById(user_id);
            getSession().setAttribute("user", user);
            redirect("/news");
        }else{
            setAttr("error", "未知错误，请登录后重试！");
            redirect("/signup");
        }
    }

    public void valid_email(){
        setAttr("title","邮箱验证");
        setAttr("param","register");
        setAttr("valid_email",getSession().getAttribute("valid_email"));
        render("/views/account/valid_email.html");
    }
}
